/* LoginWindow
 * 
 * Init()
 * Show()
 * Hide()
 * Validate()
 * Login()
 * Register()
 */

/// <reference path="../base.js" />
/// <reference path="./window.js" />
/// <reference path="../3rdParty/Mootools-Core.js" />
/// <reference path="../3rdParty/Mootools-More.js" />
/// <reference path="../3rdParty/Clientcide.js" />


Form.Validator.add("confirm", {
  errorMsg: function(element, props)
  {
    return "This field must match the previous field <b>exactly</b>!";
  },
  test: function(element, props)
  {
    return element.get("value") == $(props.confirm).get("value");
  }
});

Form.Validator.add("alphanum", {
  errorMsg: function(element)
  {
    return "Only letters, numbers, decimals, dashes, and underscores are allowed.";
  },
  test: function(element)
  {
    return !element.get("value").match(/[^A-Za-z0-9 _\.-]/);
  }
});

LoginWindow = {};

LoginWindow.Init = function()
{
  LoginWindow.Window = new UIWindow({ name: "LoginWindow",
    caption: "<a href='javascript:void(0);' class='navlink_on' id='LoginTabLink'>Login</a>&nbsp;&nbsp;&nbsp;&nbsp"
           + "<a href='javascript:void(0);' class='navlink_off' id='RegisterTabLink'>Register</a>",
    content: "<span id='LoginTab'>"
           + "<span id='LoginError' style='color: red; display: none;'></span>"
           + "<form id='LoginForm'><table>"
           + "<tr><td>Username:</td><td><input type='text' id='LoginUsername' class='required maxLength:30 minLength:6 alphanum' maxlength='30' /></td></tr>"
           + "<tr><td>Password:</td><td><input type='password' id='LoginPassword' class='required maxLength:30 minLength:6 alphanum' maxlength='30' /></td></tr>"
           + "<tr><td>&nbsp;</td><td><input type='button' onclick='LoginWindow.Login();' value='Login' /></td></tr>"
           + "</table></form>"
           + "</span>"
           + "<span id='RegisterTab'>"
           + "<span id='RegisterError' style='color: red; display: none;'></span>"
           + "<form id='RegisterForm'><table>"
           + "<tr><td>Username:</td><td><input type='text' id='RegisterUsername' class='required maxLength:30 minLength:6 alphanum' maxlength='30' /></td></tr>"
           + "<tr><td>Password:</td><td><input type='password' id='RegisterPassword' class='required maxLength:30 minLength:6 alphanum' maxlength='30' /></td></tr>"
           + "<tr><td>Confirm:</td><td><input type='password' id='RegisterConfirm' class='required maxLength:30 minLength:6 alphanum confirm:\"RegisterPassword\"' maxlength='30' /></td></tr>"
           + "<tr><td>&nbsp;</td><td><input type='button' onclick='LoginWindow.Register();' value='Register' /></td></tr>"
           + "</table></form>"
           + "</span>",
    modal: true,
    closeButton: false
  });

  LoginWindow.TabControl = new TabSwapper({
    rearrangeDOM: false,
    selectedClass: "navlink_on",
    deselectedClass: "navlink_off",
    tabs: [$("LoginTabLink"), $("RegisterTabLink")],
    sections: [$("LoginTab"), $("RegisterTab")],
    smooth: true,
    smoothSize: true
  });

  $("LoginForm").addEvent("keyup", function(event)
  {
    if (event.key == "enter")
    {
      LoginWindow.Login();
      event.stop();
    }
  });

  $("RegisterForm").addEvent("keyup",
   function(event)
   {
     if (event.key == "enter")
     {
       LoginWindow.Register();
       event.stop();
     }
   });

  LoginWindow.LoginValidator = new Form.Validator.Tips($("LoginForm"), { scrollToErrorsOnSubmit: false });
  LoginWindow.RegisterValidator = new Form.Validator.Tips($("RegisterForm"), { scrollToErrorsOnSubmit: false });
}

LoginWindow.Show = function()
{
  if (!LoginWindow.Window)
    LoginWindow.Init();

  LoginWindow.Window.show();
  $("LoginUsername").focus();
}

LoginWindow.Hide = function()
{
  LoginWindow.Window.hide();
}

LoginWindow.Validate = function()
{
  var tab = LoginWindow.TabControl.now;

  if (tab == 0) // LoginTab
  {
    return LoginWindow.LoginValidator.validate();
  }
  else if (tab == 1) // RegisterTab
  {
    return LoginWindow.RegisterValidator.validate();
  }
  else
    return false;
}

LoginWindow.Login = function()
{
  if (!LoginWindow.Validate())
    return;

  var username = $("LoginUsername").get("value");
  var password = $("LoginPassword").get("value");

  Utility.Server.Login(username, password, function(result, error)
  {
    if (!error)
    {
      $("LoginError").hide();
      LoginWindow.Hide();
    }
    else
    {
      $("LoginError").set("html", "Username / Password is Incorrect");
      $("LoginError").reveal();
    }
  });
}

LoginWindow.Register = function()
{
  if (!LoginWindow.Validate())
    return;

  var username = $("RegisterUsername").get("value");
  var password = $("RegisterPassword").get("value");

  Utility.Server.Register(username, password, function(result, error)
  {
    if (!error)
    {
      $("RegisterError").hide();
      LoginWindow.Hide();
    }
    else
    {
      $("RegisterError").set("html", "Username Already in Use");
      $("RegisterError").reveal();
    }
  });
}